package com.jinglvhuanjing.app.utils;

import android.os.Environment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class LogRecordUtil {
    // 用于格式化日期,作为日志文件名的一部分
    private static DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",
            Locale.getDefault());

    public LogRecordUtil() {
    }

    public static void saveLogInfoToFile(String text, String fileName) {
        StringBuffer sb = new StringBuffer();
        String time = getCurrentTime();
        sb.append("\n");
        sb.append(time);
        sb.append("\n");
        sb.append(text);
        //String fileName = "record-" + "logInfo" + ".log";
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            String path = Constants.SD_PATH;

            File dir = new File(path);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            File file = new File(path + fileName);
            if (!file.exists() || !file.isFile()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            try {
                // 打开一个随机访问文件流，按读写方式
                RandomAccessFile randomFile = new RandomAccessFile(path + fileName, "rw");
                // 文件长度，字节数
                long fileLength = randomFile.length();
                // 将写文件指针移到文件尾。
                randomFile.seek(fileLength);
                randomFile.write(sb.toString().getBytes());
                randomFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void replaceTextToFile(String text, String fileName){
        StringBuffer sb = new StringBuffer();
        sb.append(text);
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            String path = Constants.SD_PATH;
            File dir = new File(path);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            File file = new File(path+fileName);
            if(!file.exists() || !file.isFile()){
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                FileOutputStream fos = new FileOutputStream(path + fileName, false);
                OutputStreamWriter write = new OutputStreamWriter(fos, "UTF-8");
                write.write(sb.toString());
                write.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static String getCurrentTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
        return df.format(new Date());
    }

}
